﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;

using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;


namespace ZF.WS.shop
{
    public partial class view : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {




                var ptlist = DictionaryConfig.MTypeJsonList.Select(p => p.MType)
                    .Where(p => p.ParentId == 0).Select(p => new SimpleTree<int>()
                    {
                        Id = p.MerchandiseTypeId,
                        Name = p.Name,
                        ParentId = 0,
                    }).ToList();

                DictionaryConfig.MTypeJsonList.Select(p => p.MType)
                .ToList().ForEach(p =>
                {
                    if (p.ParentId != 0)
                    {
                        var parent = ptlist.FirstOrDefault(pt => pt.Id == p.ParentId);
                        if (parent != null)
                        {
                            parent.Childs.Add(new SimpleTree<int>()
                            {
                                Id = p.MerchandiseTypeId,
                                Name = p.Name,
                                ParentId = p.ParentId,
                            });
                        }
                    }
                });
                this.lvPTitle.DataSource = ptlist;
                this.lvPTitle.DataBind();

                var hotList = dc.SP_Merchandise.Where(p => p.IsHot && !p.IsDel)
                        .OrderByDescending(p => p.MerchandiseId)
                        .OrderByDescending(p => p.OrderID)
                        .OrderByDescending(p => p.IsHot)
                        .OrderByDescending(p => p.IsRecommend)
                        .ToList().Select(p => new
                {
                    Merc = p,
                    Photo = p.FirstPhoto,
                }).Take(2).ToList();

                this.lvHot.DataSource = hotList;
                this.lvHot.DataBind();

                var isRecommendList = dc.SP_Merchandise.Where(p => p.IsRecommend && !p.IsDel)
                        .OrderByDescending(p => p.MerchandiseId)
                        .OrderByDescending(p => p.OrderID)
                        .OrderByDescending(p => p.IsHot)
                        .OrderByDescending(p => p.IsRecommend)
                    .ToList().Select(p => new
                {
                    Merc = p,
                    Photo = p.FirstPhoto,
                })
                .Take(2).ToList();
                this.lvIsRecommend.DataSource = isRecommendList;
                this.lvIsRecommend.DataBind();


                this.lvPType.DataSource = DictionaryConfig.MTypeJsonList.Where(p => p.MType.ParentId == 0).Select(p => new
                {
                    Key = p.MType.MerchandiseTypeId,
                    Text = p.MType.Name,
                    ParentId = p.MType.ParentId,
                });
                this.lvPType.DataBind();

                var typelist = DictionaryConfig.MTypeJsonList.Select(p => new
                {
                    ParentId = p.MType.ParentId,
                    Key = p.MType.MerchandiseTypeId,
                    Text = p.MType.Name,
                }).ToList();
                this.litTypeJson.Text = Newtonsoft.Json.JsonConvert.SerializeObject(typelist);

            }
        }
    }
}
